﻿

function clsCICConnect(server)
{   
    return{              
          Connect:function(loginobj,evt){
            server.EICConn = new ActiveXObject("ININ.EICServer3.1");
			server.EICConn.ForceDisconnect(loginobj.User,loginobj.Station,true);			
			ret=server.EICConn.FullConnect('test',loginobj.Server,loginobj.User, loginobj.Password, loginobj.Station, 1,1);

			// Connect to queue
			//alert(ret);

			server.Queue = new ActiveXObject("EICQueue.EICQueue.1");
			 
			server.Queue.Connect(QueueType,loginobj.User);  
			if (ret!=0)
			{
				return ret;
			}

			// Set up the Queue2WatcherAdapter
			server.WatcherAdapter = new ActiveXObject("ClientCOM2.EICQueue2WatcherAdapter.1");
			//                  this.WatcherAdapter.ObjectAddedFunc=QueueObjectAdded;
			//                  this.WatcherAdapter.ObjectChangedFunc=QueueObjectChanged;
			//                  this.WatcherAdapter.ObjectRemovedFunc=QueueObjectRemoved;
			//                  this.WatcherAdapter.StatisticsUpdatedFunc=QueueStatsUpdated;
			server.WatcherAdapter.ObjectAddedFunc=evt.ObjectAdd;
			server.WatcherAdapter.ObjectChangedFunc=evt.ObjectChanged;
			server.WatcherAdapter.ObjectRemovedFunc=evt.ObjectRemoved;
			server.WatcherAdapter.StatisticsUpdatedFunc=evt.SetStatisticsUpdated;
			


			server.Queue.SetMarshalledCallback(server.WatcherAdapter);

			server.User=loginobj.User;
			return 0;
          }
          
        };
    
}



function clsCICUser()
{
    return{
        getStatus:function()
        {
            try
            {
                tmpUser = new ActiveXObject("ClientCOM2.EICUserS.1");
                
                tmpUser.Id = Grandsys.User;
                
                return tmpUser.StatusMessage;
            }
            catch(e)
            {
            }
        },
        setStatus:function(Status)
        {
            try
            {
            	//alert(Status);
                tmpUser = new ActiveXObject("ClientCOM2.EICUserS.1");
                
                tmpUser.Id = Grandsys.User;
                
                tmpUser.StatusKey = Status;
                
                return 0;
                
            }
            catch(e)
            {
            	return -999;
            }
        },
        setStatusCode:function(StatusCode)
        {
            try
            {
            	//alert(1);
                tmpUser = new ActiveXObject("ClientCOM2.EICUserS.1");
                
                tmpUser.Id = Grandsys.User;
                
				//alert(StatusCode);
				switch (StatusCode)
                    {
                        case 10001 : //AgentNotReady
                            {
                                 //tmpUser.StatusKey = "AgentNotReady";
                                 tmpUser.StatusKey = "Do Not Disturb";
                                 
                                break;
                            }
						case 10002 : //ready
                            {
                                //tmpUser.StatusKey = "AgentAvailable";
                                tmpUser.StatusKey = "Available";
                                 
                                break;
                            }
						case 20011 : //acwIn —话后处理(Inbound)
                            {
                                //tmpUser.StatusKey = "acwIn";
                                tmpUser.StatusKey = "Follow Up";
                                 
                                break;
                            }
						case 20012 : //话后处理(Outbound)
                            {
                                //tmpUser.StatusKey = "acwout";
                                tmpUser.StatusKey = "Follow Up";
                                 
                                break;
                            }
						/*
						case 20008 : //话后处理(Outbound)
                            {
                                //tmpUser.StatusKey = "acwout";
                                tmpUser.StatusKey = "Follow Up";
                                this.softPhoneEvent('10001'); 
                                break;
                            }
                            										
						case 14 : //用餐
                            {
                                tmpUser.StatusKey = "At Lunch";
                                 
                                break;
                            }										
						case 13 : //會議
                            {
                                tmpUser.StatusKey = "In a Meeting";
                                 
                                break;
                            }										
						case 12 : //訓練
                            {
                                tmpUser.StatusKey = "At a Training Session";
                                 
                                break;
                            }			
                            */							
					}
                
            }
            catch(e)
            {
            }
        },
        getAllStatus:function()
        {
            try
            {
                tmpUser = new ActiveXObject("ClientCOM2.EICUserS.1");
                
                tmpUser.Id = Grandsys.User;
                var Status = new   VBArray(tmpUser.AvailableStatusMessages) ;
                var ret=Status.toArray();
                ret.length=Status.ubound(1);

				var arrayObj = new Array(ret.length);
				for(i=0;i<ret.length;i++)
				  {
					var StatusItem = new VBArray(ret[i]);
					var item = StatusItem.toArray();

					arrayObj[i]=item;
				  }


                /* return array item
                
                Status (string)

                DND (bool)

                HasDate (bool)

                HasTime (bool)

                Icon File Name (string)

                ACD (bool)

                Selectable (bool)

                Persistent (bool)

                Deleted (bool)

                Forward (bool)

                ACW (bool)

                AllowFollowup (bool)

                KeyName(string)


                */
                return arrayObj;
                
                
            }
            catch(e)
            {
            }
        }
    }
}

var myEvent;
var EventUser = new ActiveXObject("ClientCOM2.EICUserS.1");
var Eventtype;


function clsCICQueueEvent()
{   
    var AddedFunc;
    var ChangedFunc;
    var RemovedFunc;
    var UpdatedFunc;
    var callList = new clsCallList();
	var CustomEvent;
	var eventtype= new getCusTomEventType();

	Eventtype=eventtype;
	
    return{              
          SetObjectAddedFunc:function(func){
            AddedFunc=func;
          },
          SetObjectChangedFunc:function(func){
            ChangedFunc=func;
          },
          SetObjectRemovedFunc:function(func){
            RemovedFunc=func;
          },
          SetStatisticsUpdatedFunc:function(func){
            UpdatedFunc=func;  
          }, 
		  SetCustomEventFunc:function(func)
		  {
			 CustomEvent=func;  
			 myEvent=CustomEvent;
		  },
          ObjectAdd:function(queue, queueObj){
            if(queueObj!=null)
            {
            
            	
                var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");
                
                
                
                tempCallObj=queueObj;
                //被转接电话
                if(tempCallObj.State==105 && tempCallObj.Id!=eventtype.getCallid())
                {
                	eventtype.setCallid(tempCallObj.Id,tempCallObj.State);
                }
               
                
                var call1 =new Callobj();
                call1.Id = tempCallObj.Id;
                call1.Status =tempCallObj.State;
                call1.StatusName =tempCallObj.StateString;
                callList.Add(call1);
                AddedFunc(queue, queueObj);

				var calltools=new clsCallTool();		
				
				var code=eventtype.getType(tempCallObj.Id,tempCallObj.State,tempCallObj.StateString,calltools.GetCallAttribute(tempCallObj.Id,"Eic_CallType"),calltools.GetCallAttribute(tempCallObj.Id,"Eic_CallDirection"),tempCallObj.isParty);
				if(code!=-1)
				{
					var user=new clsCICUser();
					user.setStatusCode(code);
					CustomEvent(queueObj,code);					
				}

				//CustomEvent(queueObj,eventtype.getType(tempCallObj.Id,tempCallObj.State,tempCallObj.StateString,calltools.GetCallAttribute(tempCallObj.Id,"Eic_CallType")));	


             }    
          },
          ObjectChanged:function(queue, queueObj){
          	//alert("ObjectChanged--------");
            //myEICConferenceCall = new ActiveXObject("EICConference.EICConference.1");
           //myEICConferenceCall.Create(queueObj);
            if(queueObj!=null)
            {
                var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");
                tempCallObj=queueObj;
                var call1 =new Callobj();
                call1.Id = tempCallObj.Id;
                call1.Status =tempCallObj.State;
                call1.StatusName =tempCallObj.StateString;
                callList.Change(call1);
                ChangedFunc(queue, queueObj);

				var calltools=new clsCallTool();
				var code=eventtype.getType(tempCallObj.Id,tempCallObj.State,tempCallObj.StateString,calltools.GetCallAttribute(tempCallObj.Id,"Eic_CallType"),calltools.GetCallAttribute(tempCallObj.Id,"Eic_CallDirection"),tempCallObj.isParty);
				if(code!=-1)
				{
					var user=new clsCICUser();
					user.setStatusCode(code);
					CustomEvent(queueObj,code);
					
				}
            } 
          },
          ObjectRemoved:function(queue, objID){
		  //alert("ObjectRemove+++");
            callList.Remove(objID);
            RemovedFunc(queue, objID);
			eventtype.removeCallid(objID);
          },
          SetStatisticsUpdated:function(queue, queue){
            UpdatedFunc(queue)  
          },
		  SetUserEvent:function(status)
		  {
				CustomEvent('',eventtype.getUserType);
		  },
          CallLists : callList
        };
    
}




function clsCICCallTool()
{
    return{
          GetCallAttribute: function(id,name)
            {
                /*
                    System Attribute:
                    
                    CallId:1001732616
                    Eic_AcdWorkgroup:_SystemRoutingHub_
                    Eic_CallDirection:I
                    Eic_CallId:1001732616
                    Eic_CallIdKey:100173261620080306
                    Eic_CallStateString:%13668%
                    Eic_CallType:I
                    Eic_Language:en-US
                    Eic_LocalName:System
                    Eic_LocalPartyType:I
                    Eic_RemoteAddress:8803
                    Eic_RemoteId:8803
                    Eic_RemoteName:U803
                    Eic_RemotePartyType:I
                    Eic_State:S
                    Eic_Workgroup:Workgroup Queue:_SystemRoutingHub_
                    Eic_WorkgroupName:_SystemRoutingHub_
                    Eic_WorkgroupQueueTimestamp:20080306011919Z
                    Eic_InitiationTime:20080306011919Z
                    Eic_IntercomParty:1001732615
                    Eic_StateTimestamp:20080306011919Z
                    Eic_AnswerTime:20080306011919Z
                    Eic_RemoteNamePriority:040
                    Eic_RemoteNameCopy:U803
                    Eic_RemoteAddressPriority:040
                    Eic_RemoteAddressCopy:8803
                    Eic_RemoteIdPriority:040
                    Eic_RemoteIdCopy:8803
                    Eic_CallState:Connected
                    Eic_RemoteTnRaw:8803
                    Eic_RemoteTnNormalized:/8803
                    Eic_OwnershipToken:100259:0
                    Eic_ConnectDurationTime:-1
                    Eic_ConnectEvtTime:00/00/0000 00:00:00
                    Eic_DNISRoutedCall:Yes
                    Eic_IntercomPartnerCallId:1001732615
                    Dir_SourceSideEntered:12849239959812
                    
                
                */
            
            
                try{
                    var tempCallObj = new ActiveXObject("ClientCOM2.EICCallObject2.1");
                    tempCallObj.Id=id;
                    return tempCallObj.GetNamedAttributeS(name);
                }
                catch(e)
                 {
                    //alert(e);
                 }
                
            },
          GetCallState: function(id)
            {
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");
                    tempCallObj.Id=id;
                    return tempCallObj.State;
                }
                catch(e)
                 {
                    //alert(e);
                 }
                
            },    
          Pickup: function(id)
            {
            //alert("電話接通的CallId=="+id);
            
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");
                    tempCallObj.Id=id;
                    tempCallObj.Pickup(Grandsys.User);
                }
                catch(e)
                 {
                    //alert(e);
                 }
                
            },
          Disconnect: function(id)
            {
            //alert("電話掛斷的CallId__"+id);
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");
                  
                    
                    tempCallObj.Id=id;
                    tempCallObj.Disconnect(Grandsys.User);
                }
                catch(e)
                 {
                 }
                
            },
         Hold: function(id)
            {
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");                
                    
                    tempCallObj.Id=id;
                    tempCallObj.Hold("", Grandsys.User);
                }
                catch(e)
                 {
                 }
            },
        Mute: function(id)
            {
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1");                
                    
                    tempCallObj.Id=id;
                    tempCallObj.Mute("");                  
                }                 
                catch(e)
                 {
                 }
                
            },
        Dail: function(number)
            {
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1"); 
                    tempCallObj.Dial(number, false);
                    
                    return tempCallObj.Id;
                 }
                 catch(e)
                 {
                    //alert(e);
                    return -999;
                 }
                
            },
        Transfer: function(number,id)
            {
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1"); 
                    tempCallObj.Id=id;
                    tempCallObj.BlindTransfer(number, "");
                    //alert(number);
                 }
                 catch(e)
                 {
                    //alert(e);
                 }
                
            },
        ConsultTransfer: function(id,consultid)
            {
                try{
                    var tempCallObj = new ActiveXObject("EICCallObject.EICCallObject.1"); 
                    tempCallObj.Id=id;
                    var result = tempCallObj.ConsultTransfer(consultid, "");
                    
                    return 0;
                 }
                 catch(e)
                 {
                    //alert(e);
                    return -999;
                 }
                
            },
        Conference: function(id,consultid)
            {
                try{
                    
                    var tempCallObj = new ActiveXObject("ClientCOM2.EICCallObject2.1"); 
                    tempCallObj.Id=id;
    
                    //Create a conference call object
                    var EICConferenceCall = new ActiveXObject("EICConference.EICConference.1"); 
                    
                    var tempCallObj2 = new ActiveXObject("ClientCOM2.EICCallObject2.1"); 
                    tempCallObj2.Id=consultid;
                    
                    EICConferenceCall.Create(tempCallObj2);                     
                    EICConferenceCall.Add(tempCallObj);
                    //test(id,consultid)   
                    
                    //return  EICConferenceCall.Id;
                    
                    
                    //alert(number);
                 }
                 catch(e)
                 {
                    //alert(e);
                 }
                
            }
        
           
          
        };
}



function getCusTomEventType()
{
	var currentcallid=0;
	var lastsataus='';
	var lastcalltype='';


	return{
		setCallid:function(callid,status)
		{
			currentcallid=callid;
			lastsataus=status;
		},
		getCallid:function()
		{
			return currentcallid;
		},
		removeCallid:function(callid)
		{
			if(currentcallid==callid)
			{
				currentcallid=0;
				lastsataus='';
			}
		},
		getUserType:function(status)
		{
			if(currentcallid==0 && lastsataus!=status)
			{
				lastsataus=status;
				
				if(status=="Available")
				{
					return 10002;
				}
				//else if(status=="AgentNotReady")
				else if(status=="Do Not Disturb")
				{
					return 10001;
				}
				else if(status=="Away from desk")
				{
					return 10001;
				}
				else if(status=="At Lunch")
				{
					return 10001;
				}
				else if(status=="Follow Up")
				{
					return 20011;//—话后处理(Inbound) 
				}
				//else if(status=="acwout")
				else if(status=="Follow Up")
				{
					return 20012;//——话后处理(Outbound)
				}
				else if(status=="AcdAgentNotAnswering")
				{
					return 20009;//——话后处理(Outbound)
				}
				else 
				{
					return 10001;
				}
			}
			else
			{
				return -1;
			}

		},
		getType: function(callid,callstatus,callstatusname,calltype,calldirection,isParty)
		{
			//alert("=---callstatus--" + callstatus + "-calltype --- " + calltype + "--calldirection---" + calldirection);
			if (currentcallid==0)
			{
				currentcallid=callid;
			}
			if(currentcallid==callid)
			{

				

				/*Alerting = 1

                Connected = 105

                Dialing = 103

                Disconnected = 106

                Initializing = 100

                Manual Dialing = 102

                Offering = 101

                On Hold = 4

                Proceeding = 104

                Station Audio = 107
                */

				switch (callstatus)
                    {
                        case 1 : //Alerting
                            {
								if(calltype=="I")
								{
									return 20000;
								}
								else
								{
									return 20001;
								}
                                 
                                break;
                            }
					    case 4 : //On Hold 
                            {
								if(lastsataus!=20013)
								{
									return 20005;
								}
								else
								{
									return -1;
								}
								
                                 
                                break;
                            }
						case 101 : //Alerting
                            {
                                if(calltype=="I")
								{
									return 20000;
								}
								else
								{
									return 20001;
								}
                                 
                                break;
                            }
						case 106 : //Disconnected
                            {
								
								//alert(calltype);
								//alert(callstatus);
								//alert(calldirection);
								currentcallid=0;
								lastsataus=0;
	
								if(calltype=="E")//E:表示外线;I:内线
								{
									if(calldirection=="I")
									{
										lastcalltype=""
										return 20011;
									}
									else 
									{
										lastcalltype=""
										return 20012;
									}
								}							
								else
								{
									if(isParty)
									{
										//alert(lastcalltype);
										if(lastcalltype=="I")
										{
											lastcalltype="";
											return 20011;
										}
										else if(lastcalltype=="E")
										{
											lastcalltype="";
											return 20012;
										}
										else
										{
											lastcalltype="";
											return 20008;
										}
									}
									else
									{
										//lastcalltype=""
										return 20008;
									}
								}
								
                                 
                                break;
                            }
						case 102 : //Dialing
                            {
								if(lastsataus!=20013)
								{				
									return 20002;
								}
								else
								{
									return -1;
								}
                                 
                                break;
                            }
						case 103 : //Dialing
                            {
								if(lastsataus!=20013)
								{
									return 20002;
								}
								else
								{
									return -1;
								}
                                 
                                break;
                            }
						case 100 : //Dialing
                            {
								if(lastsataus!=20013)
								{
									return 20002;
								}
								else
								{
									return -1;
								}
                                 
                                break;
                            }
						case 105 : //Connected
                            {
                                
								if(isParty)
								{
									return -1;
								}
								else
								{
									if(lastsataus!=20013)
									{
										if(calltype=="I")
										{
											lastcalltype="I";
											return 20003;
										}
										else
										{
											lastcalltype="E";
											return 20004;
										}
									}
									else
									{
										//lastcalltype="O";
										return 20006;
									}
								}
								                                
                                break;
                            }
					}

			}
			else
			{
				return -1;
			}

			

		}
	}

}


function userevent()
{
	try
	{
		//alert('1-------------------');
		//var user=new clsUser();
		//alert(user.getStatus());	
		EventUser.Id = Grandsys.User;     
		var eventtype= Eventtype;
		//alert('1-------EventUser.StatusKey------------' + EventUser.StatusKey +'=====' + EventUser.StatusMessage);
		var code=eventtype.getUserType(EventUser.StatusKey);
		if(code!=-1)
		{
			CustomEvent('',code);
		}
		setTimeout("userevent()", 3000);
	}
	catch (e)
	{
		//alert(e.message);
		setTimeout("userevent()", 3000);
	}
	
	//CustomEvent('',user.getStatus());
}

setTimeout("userevent()", 3000);

